package thread.demo01;

/**
 * @author 真香
 * @create 2021-06-02 16:54
 */

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 联系Thread ，实现网络图片的下载
 *
 */

public class TestThread2 extends Thread  {


    // 网络图片地址
    private String url;

    // 保存文件名
    private String name;


    public TestThread2(String url,String name) {
        this.url = url;
        this.name = name;
    }


    /**
     * 下载图片的线程执行体
     */
    @Override
    public void run() {

        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载了文件名为"+name);

    }
    public static void main(String[] args){

        TestThread2 thread1 = new TestThread2("https://pics0.baidu.com/feed/91529822720e0cf30c0b69c1b1506a17be09aa2f.jpeg?token=132622209e1168d74b3cc465666e6116","鸿蒙概念图1.jpg");
        TestThread2 thread2 = new TestThread2("https://pics0.baidu.com/feed/91529822720e0cf30c0b69c1b1506a17be09aa2f.jpeg?token=132622209e1168d74b3cc465666e6116","鸿蒙概念图2.jpg");
        TestThread2 thread3 = new TestThread2("https://pics0.baidu.com/feed/91529822720e0cf30c0b69c1b1506a17be09aa2f.jpeg?token=132622209e1168d74b3cc465666e6116","鸿蒙概念图3.jpg");

        thread1.start();
        thread2.start();
        thread3.start();

    }

}


class WebDownLoader {


    /**
     * 下载方法
     * @param url
     * @param name
     */
    public void downloader(String url,String name){

        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");


        }
    }


}
